<?php

namespace App\common\Plugins\ThirdPartyLogin\Controllers;

use App\Http\Controllers\Controller;
use App\common\Plugins\ThirdPartyLogin\Lib\WeChatApi;
use Illuminate\Http\Request;

class InitController extends Controller {
    //构造函数
    public function __construct(Request $request) {
        parent::__construct($request);
    }

    //微信code
    public static function weChatCode($event) {
        $clapi = new WeChatApi();
        $config = json_decode(file_get_contents(dirname(__DIR__) . '/config.json'), true);
        if ($event->login_type == 'public') {//公众号
            $clapi->setConfig('APPID', $config['wx_PUBLIC_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_PUBLIC_APPSECRET']);
            return $clapi->publicGetCode($event->data);
        } elseif ($event->login_type == 'small') {//小程序

        } elseif ($event->login_type == 'pc') {//PC/电脑端
            $clapi->setConfig('APPID', $config['wx_PC_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_PC_APPSECRET']);
            return $clapi->pcGetCode($event->data);
        } else {
            return ["msg" => '请求类型错误', "status" => 0];
        }

    }

    public static function codeGetWeChatInfo($event) {
        $clapi = new WeChatApi();
        $config = json_decode(file_get_contents(dirname(__DIR__) . '/config_'.$event->platform.'.json'), true);

        if ($event->login_type == 'public') {//公众号
            $clapi->setConfig('APPID', $config['wx_PUBLIC_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_PUBLIC_APPSECRET']);
            return $clapi->publicCodeGetWeChatInfo($event->data);
        } elseif ($event->login_type == 'small') {//小程序
            $clapi->setConfig('APPID', $config['wx_SMALL_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_SMALL_APPSECRET']);
            return $clapi->smallCodeGetWeChatInfo($event->data);
        } elseif ($event->login_type == 'pc') {//电脑端
            $clapi->setConfig('APPID', $config['wx_PC_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_PC_APPSECRET']);
            return $clapi->publicCodeGetWeChatInfo($event->data);
        } else {
            return ["msg" => '请求类型错误', "status" => 0];
        }

    }

    public static function publicTokenGetWeChatUserInfo($event) {
        $clapi = new WeChatApi();
        if ($event->login_type == 'public') {//公众号
            return $clapi->publicTokenGetWeChatUserInfo($event->data);
        } elseif ($event->login_type == 'small') {//小程序
            return $clapi->smallTokenGetWeChatUserInfo($event->data);
        } else {
            return ["msg" => '请求类型错误', "status" => 0];
        }
    }

    public static function share($event) {
        $clapi = new WeChatApi();
        $config = json_decode(file_get_contents(dirname(__DIR__) . '/config.json'), true);
        if ($event->login_type == 'public') {//公众号
            $clapi->setConfig('APPID', $config['wx_PUBLIC_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_PUBLIC_APPSECRET']);
            return $clapi->publicShare($event->data);
        } elseif ($event->login_type == 'small') {//小程序
            $clapi->setConfig('APPID', $config['wx_SMALL_APPID']);
            $clapi->setConfig('APPSECRET', $config['wx_SMALL_APPSECRET']);
            return $clapi->generatingQROfSmallProgram($event->data);
        } else {
            return ["msg" => '请求类型错误', "status" => 0];
        }
    }
}


